package cn.bisoft.java.service.ext.httpclient;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import cn.bisoft.java.bean.entity.Account;
import cn.bisoft.java.bean.entity.AnteCardFill;
import cn.bisoft.java.bean.entity.Drawing;
import cn.bisoft.java.bean.entity.Fill;
import cn.bisoft.java.bean.entity.Issue;
import cn.bisoft.java.bean.entity.Present;
import cn.bisoft.java.bean.entity.Ticket;
import cn.bisoft.java.bean.entity.UniteAnteBuy;
import cn.bisoft.java.bean.entity.UniteAnteCommision;
import cn.bisoft.java.bean.entity.UniteAnteInfo;
import cn.bisoft.java.bean.entity.UniteAnteTicket;
import cn.bisoft.java.bean.message.MessageHeader;
import cn.bisoft.java.bean.message.TransMessage;
import cn.bisoft.java.bean.message.TransType;
import cn.bisoft.java.bean.message.request.AccountQuery;
import cn.bisoft.java.bean.message.request.BalanceQuery;
import cn.bisoft.java.bean.message.request.BonusQuery;
import cn.bisoft.java.bean.message.request.CardFillRequest;
import cn.bisoft.java.bean.message.request.DrawingQuery;
import cn.bisoft.java.bean.message.request.DrawingRequest;
import cn.bisoft.java.bean.message.request.FillQuery;
import cn.bisoft.java.bean.message.request.IssueQuery;
import cn.bisoft.java.bean.message.request.LotteryRequest;
import cn.bisoft.java.bean.message.request.PresentMoney;
import cn.bisoft.java.bean.message.request.PresentQuery;
import cn.bisoft.java.bean.message.request.RiskControlQuery;
import cn.bisoft.java.bean.message.request.TicketQuery;
import cn.bisoft.java.bean.message.request.UniteAnteQueryRequest;
import cn.bisoft.java.bean.message.request.UniteAnteRequest;

import com.hengpeng.common.msg.agentportal.MessageConstant;



public class MessageBuiler
{
	public static TransMessage createAccountQueryMessage(String version, long sequece, String counterId,
			Set<String> accountIds)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_ACCOUNTQUERY_REQUEST);

		AccountQuery body = new AccountQuery();
		Set<Account> accounts = new HashSet<Account>();
		for (String accountId : accountIds)
		{
			Account account = new Account();
			account.setUserName(accountId);
			accounts.add(account);
		}
		body.setAccount(accounts);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createAnteCardFillMessage(String version, long sequece, String counterId,
			AnteCardFill anteCardFill)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_CARDFILLREQUEST_REQUEST);

		CardFillRequest body = new CardFillRequest();
		body.setAnteCardFill(anteCardFill);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createBalanceQueryMessage(String version, long sequece, String counterId,
			String frontGameId, String issueName)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_BALANCEQUERY_REQUEST);

		BalanceQuery body = new BalanceQuery();
		Issue issue = new Issue();
		issue.setGameName(frontGameId);
		issue.setNumber(issueName);
		body.setIssue(issue);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createBonusQueryMessage(String version, long sequece, String counterId,
			String frontGameId, String issueName)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_BONUSQUERY_REQUEST);

		BonusQuery body = new BonusQuery();
		Issue issue = new Issue();
		issue.setGameName(frontGameId);
		issue.setNumber(issueName);
		body.setIssue(issue);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createDrawingQueryMessage(String version, long sequece, String counterId,
			Set<String> drawingIds)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_DRAWINGQUERY_REQUEST);

		DrawingQuery body = new DrawingQuery();
		Set<Drawing> drawings = new HashSet<Drawing>();
		for (String drawingId : drawingIds)
		{
			Drawing drawing = new Drawing();
			drawing.setId(drawingId);
			drawings.add(drawing);
		}
		body.setDrawing(drawings);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createDrawingRequestMessage(String version, long sequece, String counterId,
			Set<Drawing> drawings)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_DRAWINGREQUEST_REQUEST);

		DrawingRequest body = new DrawingRequest();
		body.setDrawing(drawings);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createFillQueryMessage(String version, long sequece, String counterId,
			Set<String> fillIds)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_FILLQUERY_REQUEST);

		FillQuery body = new FillQuery();
		Set<Fill> fills = new HashSet<Fill>();
		for (String fillId : fillIds)
		{
			Fill fill = new Fill();
			fill.setId(fillId);
			fills.add(fill);
		}
		body.setFill(fills);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createIssueQueryMessage(String version, long sequece, String counterId,
			String frontGameId, String issueName)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_ISSUEQUERY_REQUEST);

		IssueQuery body = new IssueQuery();
		Issue issue = new Issue();
		issue.setGameName(frontGameId);
		issue.setNumber(issueName);
		body.setIssue(issue);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createLotteryQueryMessage(String version, long sequece, String counterId,
			Set<String> ticketIds)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId, TransType.TRANSTYPE_LOTTERYQUERY_REQUEST
				.value());

		TicketQuery body = new TicketQuery();
		Set<Ticket> tickets = new HashSet<Ticket>();
		for (String ticketId : ticketIds)
		{
			Ticket ticket = new Ticket();
			ticket.setId(ticketId);
			tickets.add(ticket);
		}
		body.setTicket(tickets);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createLotteryRequestMessage(String version, long sequece, String counterId,
			Set<Ticket> tickets)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_LOTTERYREQUEST_REQUEST);

		LotteryRequest body = new LotteryRequest();
		body.setTicket(tickets);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createLotteryRequestMessageById(String version, String sequece, String counterId,
			Set<Ticket> tickets)
	{
		TransMessage tm = createMessageCommonPartById(version, sequece, counterId,
				MessageConstant.TRANSTYPE_LOTTERYREQUEST_REQUEST);

		LotteryRequest body = new LotteryRequest();
		body.setTicket(tickets);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createPresentQueryMessage(String version, long sequece, String counterId,
			Set<String> presentIds)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_PRESENTQUERY_REQUEST);

		PresentQuery body = new PresentQuery();
		Set<Present> presents = new HashSet<Present>();
		for (String presendId : presentIds)
		{
			Present present = new Present();
			present.setId(presendId);
			presents.add(present);
		}
		body.setPresent(presents);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createPresentRequestMessage(String version, long sequece, String counterId,
			Set<Present> presents)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_PRESENTMONEY_REQUEST);

		PresentMoney body = new PresentMoney();
		body.setPresent(presents);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createPrizeQueryMessage(String version, long sequece, String counterId,
			String frontGameId, String issueName)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId, TransType.TRANSTYPE_PRIZEQUERY_REQUEST
				.value());

		IssueQuery body = new IssueQuery();
		Issue issue = new Issue();
		issue.setGameName(frontGameId);
		issue.setNumber(issueName);
		body.setIssue(issue);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createRiskControlQueryByPlayTypeMessage(String version, long sequece, String counterId,
			String gameName, String issueNumber, String playType)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_RISKCONTROLPLAYTYPE_REQUEST);

		RiskControlQuery body = new RiskControlQuery();
		body.setGameName(gameName);
		body.setNumber(issueNumber);
		body.setPlayType(playType);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createRiskControlQueryMessage(String version, long sequece, String counterId,
			String gameName, String issueNumber, String anteCode)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_RISKCONTROL_REQUEST);

		RiskControlQuery body = new RiskControlQuery();
		body.setAnteCode(anteCode);
		body.setGameName(gameName);
		body.setNumber(issueNumber);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createTicketQueryMessage(String version, long sequece, String counterId,
			Set<String> ticketIds)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_TICKETQUERY_REQUEST);

		TicketQuery body = new TicketQuery();
		Set<Ticket> tickets = new HashSet<Ticket>();
		for (String ticketId : ticketIds)
		{
			Ticket ticket = new Ticket();
			ticket.setId(ticketId);
			tickets.add(ticket);
		}
		body.setTicket(tickets);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createUniteAnteMessage(String version, long sequece, String counterId, String anteId,
			UniteAnteInfo uniteAnteInfo, Set<UniteAnteTicket> ticket, Set<UniteAnteBuy> buy,
			Set<UniteAnteCommision> commision)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_UNITANTEREQUEST_REQUEST);

		UniteAnteRequest body = new UniteAnteRequest();
		body.setUniteAnteInfo(uniteAnteInfo);
		body.setBuy(buy);
		body.setCommision(commision);
		body.setTicket(ticket);
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createUniteAnteQueryMessage(String version, long sequece, String counterId, String anteId)
	{
		TransMessage tm = createMessageCommonPart(version, sequece, counterId,
				MessageConstant.TRANSTYPE_UNITANTEQUERY_REQUEST);

		UniteAnteQueryRequest body = new UniteAnteQueryRequest();
		body.setCounterAnteId(anteId);
		tm.setBody(body);
		return tm;
	}

	private static TransMessage createMessageCommonPart(String version, long sequece, String counterId, int transType)
	{
		Date date = new Date();
		String datePart = new SimpleDateFormat("yyyyMMdd").format(date);
		StringBuffer messageId = new StringBuffer();
		messageId.append(counterId);
		messageId.append(datePart);
		DecimalFormat df = new DecimalFormat("00000000");
		messageId.append(df.format(sequece));

		TransMessage tm = new TransMessage();
		if (version.startsWith("0"))
		{
			tm.setVersion(MessageConstant.PROTOCOL_VERSION);
		}
		else
		{
			tm.setVersion(version);
		}
		tm.setId(messageId.toString());

		MessageHeader header = new MessageHeader();
		header.setMessengerID(counterId);
		header.setTimestamp(new SimpleDateFormat("yyyyMMddHHmmss").format(date));
		header.setTransactionType(transType);
		tm.setHeader(header);
		return tm;
	}

	private static TransMessage createMessageCommonPartById(String version, String sequece, String counterId, int transType)
	{
		TransMessage tm = new TransMessage();
		if (version.startsWith("0"))
		{
			tm.setVersion(MessageConstant.PROTOCOL_VERSION);
		}
		else
		{
			tm.setVersion(version);
		}
		tm.setId(sequece);

		MessageHeader header = new MessageHeader();
		header.setMessengerID(counterId);
		header.setTimestamp(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		header.setTransactionType(transType);
		tm.setHeader(header);
		return tm;
	}
}
